//获取应用实例
var app = getApp();
Page({
    data: {
        orderList: [],
        doingList: [],
		finishList: [],
		allList: [],
        doingState: false,
        finishState: false,
        allState: false,
        location: "doing"
    },
	  onShow: function(options) {
		  console.log(options)
	  },
    onLoad: function (option) {
    	console.log(option)
        this.queryOrderList(this,app.globalData.openId,option.bizFlag,function (list) {
            this.setData({
                orderList: list
            })
        });
    },

    queryOrderList:function (pageObj,accountId,bizFlag) {
	    var location = "doing";
	    if(bizFlag==app.globalData.bizDoingFlag){
		    location = "doing";
	    }else if(bizFlag==app.globalData.bizFinishFlag){
		    location = "finish";
	    }else if(bizFlag==app.globalData.bizAllFlag){
		    location = "all";
	    }
	    this.setData({
	      location: location
      })

      if(bizFlag==1){
	    	/*
	    	*   doingState 防止数据为空时，点击会调用接口
	    	* */
        if(this.data.doingList.length||this.data.doingState){
          this.setData({
            orderList: this.data.doingList
          })
        }else {
        this.getOrderList(pageObj,accountId,bizFlag,function(list){
	        pageObj.setData({
            orderList: list,
            doingList: list,
            doingState: true
          })
        })
        }
      }else if(bizFlag==2){
	      if(this.data.finishList.length||this.data.finishState){
		      this.setData({
			      orderList: this.data.finishList
		      })
	      }else {
		      this.getOrderList(pageObj,accountId,bizFlag,function(list){
			      pageObj.setData({
				      orderList: list,
				      finishList: list,
				      finishState: true
			      })
		      })
	      }
      }else if(bizFlag==3){
	      if(this.data.allList.length||this.data.allState){
		      this.setData({
			      orderList: this.data.allList
		      })
	      }else {
		      this.getOrderList(pageObj,accountId,bizFlag,function(list){
			      pageObj.setData({
				      orderList: list,
				      allList: list,
				      allState: true
			      })
		      })
	      }
      }
    },

	  getOrderList: function (pageObj,accountId,bizFlag,callBack) {
	    var opt = {
		    url: app.globalData.host+"/wx/ed/list.ajax",
		    data: {
			    'accountId': accountId,
			    'bizFlag': bizFlag,
			    'orderType': app.globalData.orderType
		    },
		    sucFn:function (result) {
			    callBack&&callBack(result.body.orderList)
		    }
	    };
	    app.ajax(opt);
    },

    doingTap:function (e) {
        this.queryOrderList(this,app.globalData.openId,app.globalData.bizDoingFlag);
    },
    finishTap:function (e) {
        this.queryOrderList(this,app.globalData.openId,app.globalData.bizFinishFlag);
    },
    allTap:function (e) {
        this.queryOrderList(this,app.globalData.openId,app.globalData.bizAllFlag);
    },
    orderDetailTap:function (e) {
        var orderId = e.currentTarget.dataset.id,
	          type = e.currentTarget.dataset.type;
	      var url = type==15?'../twOrderdetail/twOrderdetail':type==8?'../orderdetail/orderdetail':'';
        app.navigateTo({
            url: url+'?orderId='+orderId
        });
    }
});
